//
//  MyFamilyViewController.m
//  安好
//
//  Created by lianchuang on 14-11-19.
//  Copyright (c) 2014年 lianchuangbrother. All rights reserved.
//

#import "MyFamilyViewController.h"
#import "MainViewController.h"
#import "XHDrawerController.h"
#import "AddFamilyViewController.h"
//家人所有信息的modal
#import "AllFamilyModal.h"
//跳转到我的家人页面有“病历”
#import "TakeDrugReminderViewController.h"


#define HeaderPIC_Weight 88
#define headerPIC_Height 88




@interface MyFamilyViewController ()
/**
 *  头像的整体X的偏移量
 */
@property(nonatomic,assign)CGFloat headerOffSetX;
/**
 *  头像的整体Y的偏移量
 */
@property(nonatomic,assign)CGFloat headerOffSetY;

/**
 *  家人的个数
 */
@property(nonatomic,assign)NSInteger familyNumber;

//scrollView
@property(nonatomic,strong)UIScrollView * mainScrollView;
/**
 *  保存所有家人的信息modal
 */
@property(nonatomic,strong)NSMutableArray * AllMyFamilyArray;

@property(nonatomic,strong)AllFamilyModal * familyModal;
/**
 *  我自己的数据
 */
@property(nonatomic,strong)NSDictionary * mySelfDic;

@property(nonatomic,assign)BOOL isShake;

@end

@implementation MyFamilyViewController

- (void)viewDidLoad {
    MyNSLog("我的家人-----");
    [super viewDidLoad];
    
    self.headerOffSetY = 0;
    self.headerOffSetX = 0;
    self.isShake = NO;
    self.AllMyFamilyArray = [NSMutableArray arrayWithCapacity:0];
    UserModel * model =[[UserManager shareInstance]getUserInfo];
    self.mySelfDic = @{@"uid":model.uid,@"avatar":model.avatar};
    [self setNaviBarAttribute];
    //创建scrollView
    [self makeScrollView];
}

-(void)viewWillAppear:(BOOL)animated
{
    //先把scrollView上的所有view删除
    for (UIView * view in self.mainScrollView.subviews) {
        [view removeFromSuperview];
    }
    
    [self AFNetRequestWithAllFamily];
}

#pragma mark - AFNet--网络请求所有家人信息
//这里获取到家人的fid，名字，是否有用药提醒，家人头像
-(void)AFNetRequestWithAllFamily
{
    
    [self showActionView];
    NSString *postURL = [NSString stringWithFormat:@"%@user/home/",kBaseURL];
    //post参数列表
    NSDictionary *parameters = @{@"platform": kPlatform,@"version":kVersion};
    //网络请求管理者
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    //开始请求
    [manager POST:postURL parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        MyNSLog("成功");
        [self hiddenActionView];
        NSNumber * statusNum = [responseObject objectForKey:@"status"];
        NSNumberFormatter* numberFormatter = [[NSNumberFormatter alloc] init];
        NSString * statusString = [numberFormatter stringFromNumber:statusNum];
        if ([statusString isEqualToString:@"0"]) {
            [self.AllMyFamilyArray removeAllObjects];
            //先把scrollView上的所有view删除
            for (UIView * view in self.mainScrollView.subviews) {
                [view removeFromSuperview];
            }
            [self makeMyHeaderOnScrollView:self.mainScrollView WithOffsetWithX:0 andOffsetY:0];
        }else
        {
            MyNSLog("%@", responseObject[@"msg"]);
            [self.AllMyFamilyArray removeAllObjects];
            NSArray * dataArray = responseObject[@"data"];
            MyNSLog("家人信息%@", dataArray);
            for (NSDictionary * dic in dataArray) {
                AllFamilyModal * modal = [[AllFamilyModal alloc] initWithDic:dic];
                //把数据字典存入数组
                [self.AllMyFamilyArray addObject:modal];
            }
            //先把scrollView上的所有view删除
            for (UIView * view in self.mainScrollView.subviews) {
                [view removeFromSuperview];
            }
            [self makeDataPrepareForMakeUI];
            
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        MyNSLog("失败");
        //如果网络失败就把自己的头像添加
        [self hiddenActionView];
        [self makeMyHeaderOnScrollView:self.mainScrollView WithOffsetWithX:0 andOffsetY:0];
        
    }];
    
}//网络请求之后会把家人数据modal存入allMyFamilyDataArray 中

#pragma mark - 设置naviBar 并实现左右naviBar button方法
- (void)setNaviBarAttribute{
    
    self.navigationItem.title = @"我的家人";
    
    //设置左NaviBar
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(0, 0, 23, 23);
    [leftButton setImage:[UIImage imageNamed:@"公用_返回"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(handleLeftButtonAction) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    //设置右naviBar---触发“添加家人”
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    rightButton.frame = CGRectMake(0, 0, 23, 23);
    [rightButton setImage:[UIImage imageNamed:@"公用_添加"] forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(handleRightButtonAction) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
}

/**
 *  返回主页面
 */
- (void)handleLeftButtonAction{

    [self.navigationController popToRootViewControllerAnimated:YES];
}

//push到 “添加家人”页面AddFamilyViewController
- (void)handleRightButtonAction{
 
    if (self.AllMyFamilyArray.count < 18) {
        AddFamilyViewController *addFamily = [[AddFamilyViewController alloc] init];
        [self.navigationController pushViewController:addFamily animated:YES];
    }else{
        
        [self showCustomHudSingleLine:@"家人人数已达上限"];
    }
    
}

#pragma mark - 创建底层scrollView
-(void)makeScrollView{

    self.mainScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight-iOS7TopMargin)];
    [self.view addSubview:self.mainScrollView];
    
    //给scrollView添加手势--结束抖动
    UITapGestureRecognizer *tapScrollViewGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(EndWobble)];//点击scrollView就结束抖动
    [self.mainScrollView addGestureRecognizer:tapScrollViewGes];
    [self.view sendSubviewToBack:self.mainScrollView];

    
}
//获取数据
-(void)loadDataWithFamilyHeader
{
    [self AFNetRequestWithAllFamily];
}
/**
 *  根据家人个数创建家人头像和我自己的头像
 */
-(void)makeDataPrepareForMakeUI
{
    CGFloat headerY[18]={
        ((kScreenHeight-iOS7TopMargin)/2-44*3+12),
        ((kScreenHeight-iOS7TopMargin)/2-44*3+12),
        ((kScreenHeight-iOS7TopMargin)/2.0-44),
        ((kScreenHeight-iOS7TopMargin)/2.0+44-12),
        ((kScreenHeight-iOS7TopMargin)/2.0+44-12),
        ((kScreenHeight-iOS7TopMargin)/2.0-44),
        ((kScreenHeight-iOS7TopMargin)/2.0-44*5+12*2),
        ((kScreenHeight-iOS7TopMargin)/2.0-44*5+12*2),
        ((kScreenHeight-iOS7TopMargin)/2-44*3+12),
        ((kScreenHeight-iOS7TopMargin)/2.0-44),
        ((kScreenHeight-iOS7TopMargin)/2.0+44-12),
        ((kScreenHeight-iOS7TopMargin)/2.0+44*3-12*2),
        ((kScreenHeight-iOS7TopMargin)/2.0+44*3-12*2),
        ((kScreenHeight-iOS7TopMargin)/2.0+44*3-12*2),
        ((kScreenHeight-iOS7TopMargin)/2.0+44-12),
        ((kScreenHeight-iOS7TopMargin)/2.0-44),
        ((kScreenHeight-iOS7TopMargin)/2-44*3+12),
        ((kScreenHeight-iOS7TopMargin)/2.0-44*5+12*2),
    };
    NSMutableArray *circleHeaderFramY = [NSMutableArray arrayWithCapacity:0];
    for (NSInteger i = 0; i < 18; i++) {
        [circleHeaderFramY addObject:[NSString stringWithFormat:@"%f",headerY[i]]];
    }
    CGFloat headerX[18] = {
        (kScreenWidth/2.0 -88),
        (kScreenWidth/2.0),
        (kScreenWidth/2.0+44),
        (kScreenWidth/2.0),
        (kScreenWidth/2.0-88),
        (kScreenWidth/2.0-44*3),
        (kScreenWidth/2.0-44),
        (kScreenWidth/2.0+44),
        (kScreenWidth/2.0+88),
        (kScreenWidth/2.0+44*3),
        (kScreenWidth/2.0+88),
        (kScreenWidth/2.0+44),
        (kScreenWidth/2.0-44),
        (kScreenWidth/2.0-44*3),
        (kScreenWidth/2.0-44*4),
        (kScreenWidth/2.0-44*5),
        (kScreenWidth/2.0-44*4),
        (kScreenWidth/2.0-44*3)
    };
    NSMutableArray *circleHeaderFramX = [NSMutableArray arrayWithCapacity:0];
    for (NSInteger i = 0; i < 18; i++) {
        [circleHeaderFramX addObject:[NSString stringWithFormat:@"%f",headerX[i]]];
    }
    
    //家人位置底部有颜色的图片相邻图片之间颜色不一样
    NSArray * imgNumArray = @[@"1",@"2",@"3",@"4",@"5",@"3",@"4",@"5",@"1",@"2",@"5",@"1",@"2",@"3",@"4",@"2",@"5",@"3"];
    NSMutableArray * headerImgNameArray = [NSMutableArray arrayWithCapacity:0];
    for (NSInteger i = 0; i < imgNumArray.count; i++) {
        NSString * strimg = [NSString stringWithFormat:@"type-%@.png",imgNumArray[i]];
        [headerImgNameArray addObject:strimg];
    }
    if (self.AllMyFamilyArray.count > 18) {
        self.familyNumber = 18;
    }else{
        self.familyNumber = self.AllMyFamilyArray.count;
    }
    self.mainScrollView.contentSize = CGSizeMake(0, 0);
    self.mainScrollView.contentOffset = CGPointMake(0, 0);
    self.headerOffSetX = 0;

    CGFloat leftMinX = kScreenWidth;
    CGFloat rightMaxX = 0;
    for (NSInteger i = 0; i < self.familyNumber; i++) {
        if (leftMinX >= headerX[i]) {
            leftMinX = headerX[i];
        }
        if (rightMaxX <= headerX[i]) {
            rightMaxX = headerX[i];
        }
    }
    //所有家人头像的左右间的距离
    CGFloat headersDistance = rightMaxX - leftMinX + HeaderPIC_Weight;
    /**
     *  家人蜂窝的中心坐标
     */
    CGFloat headerCenterX = leftMinX + headersDistance/2.0;
    
    //最右边的家人的边上的坐标
    if (rightMaxX+HeaderPIC_Weight > kScreenWidth) {
        if (headersDistance > kScreenWidth) {
            self.headerOffSetX = kScreenWidth/2.0 - headerCenterX;
            self.mainScrollView.contentSize = CGSizeMake(kScreenWidth + (headersDistance - kScreenWidth)+40, 0);//加40让两边各留20
            self.mainScrollView.contentOffset = CGPointMake((headersDistance - kScreenWidth+40)/2.0, 0);
            self.headerOffSetX = self.headerOffSetX+(headersDistance - kScreenWidth+40)/2.0;
            
        }else{
            self.headerOffSetX = kScreenWidth/2.0 - headerCenterX;
        }
    }

    /*
    self.mainScrollView.contentSize = CGSizeMake(0, 0);
    self.mainScrollView.contentOffset = CGPointMake(0, 0);
    self.headerOffSetX = 0;
    if (kScreenHeight == 480 ||kScreenHeight == 568) {
        if (self.familyNumber == 9 ) {
            self.headerOffSetX = kScreenWidth/2.0 - headerCenterX;
        }
        if (self.familyNumber <=14 && self.familyNumber > 9){
            self.mainScrollView.contentSize = CGSizeMake(kScreenWidth+60, 0);
            self.mainScrollView.contentOffset = CGPointMake(30, 0);
            self.headerOffSetX = -15;
        }
        if (self.familyNumber ==15) {
            self.mainScrollView.contentSize = CGSizeMake(kScreenWidth+90, 0);
            self.mainScrollView.contentOffset = CGPointMake(45, 0);
            self.headerOffSetX = 23;
        }
        if (self.familyNumber > 15) {
            self.mainScrollView.contentSize = CGSizeMake(kScreenWidth+135, 0);
            self.mainScrollView.contentOffset = CGPointMake(67.5, 0);
            self.headerOffSetX = 66;
        }
    }
    if (kScreenHeight > 568) {
        if (self.familyNumber == 9 ) {
            self.headerOffSetX = -22;
        }
        if (self.familyNumber <=14 && self.familyNumber >= 10){
            
            self.headerOffSetX = -45;
        }
        if (self.familyNumber ==15) {
            self.mainScrollView.contentSize = CGSizeMake(kScreenWidth+66, 0);
            self.mainScrollView.contentOffset = CGPointMake(33, 0);
            self.headerOffSetX = 12;
        }
        if (self.familyNumber > 15) {
            self.mainScrollView.contentSize = CGSizeMake(kScreenWidth+88, 0);
            self.mainScrollView.contentOffset = CGPointMake(44, 0);
            self.headerOffSetX = 44;
        }
       
    }
    */
    
    [self makeCircleAddScrollView:self.mainScrollView WithFramXArray:circleHeaderFramX andFramYArray:circleHeaderFramY WithOffsetWithX:self.headerOffSetX andOffsetY:self.headerOffSetY WithImageNameArray:headerImgNameArray];
    [self makeMyHeaderOnScrollView:self.mainScrollView WithOffsetWithX:self.headerOffSetX andOffsetY:self.headerOffSetY];
    
    
}


/**
 *  //创建我自己的头像--只有在没有网络或者网络请求失败之后才创建
 *
 *  @return
 */
-(void)makeMyHeaderOnScrollView:(UIScrollView*)scrollView WithOffsetWithX:(CGFloat)offSetX andOffsetY:(CGFloat)offSetY
{
    
    CGFloat centerX = (kScreenWidth - HeaderPIC_Weight)/2.0;
    CGFloat centerY = (kScreenHeight-iOS7TopMargin)/2.0 - 44;
    
    UIButton * myBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    myBtn.frame =CGRectMake(centerX+offSetX, centerY+offSetY, HeaderPIC_Weight, headerPIC_Height);
    [myBtn setImage:[UIImage imageNamed:@"type-6.png"] forState:UIControlStateNormal];
    myBtn.clipsToBounds = YES;
    myBtn.layer.cornerRadius = 34;
    myBtn.tag = 12345;
    [myBtn addTarget:self action:@selector(familyHeaderButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [scrollView addSubview:myBtn];
    

    UIImageView * myHeader = [[UIImageView alloc] initWithFrame:myBtn.bounds];
    myHeader.userInteractionEnabled = NO;
    UIImage * userImg = [[UserManager shareInstance] getUserImage];
    
    if (userImg) {
        [myHeader setImage:userImg];
    }else{
        [myHeader setImageWithURL:[NSURL URLWithString:[[UserManager shareInstance]getUserInfo].avatar]];
    }
    [myBtn addSubview:myHeader];
    //
    UIImageView * btnTopImgView = [[UIImageView alloc] initWithFrame:myBtn.bounds];
    btnTopImgView.userInteractionEnabled = NO;
    [btnTopImgView setImage:[UIImage imageNamed:@"topCover.png"]];
    [myBtn addSubview:btnTopImgView];
}

#pragma mark - 加载我的家人的头像位置
/**
 *  加载家人头像
 *
 *  @param scrollView   底部scrollView，所有头像都贴在这个scroll上
 *  @param FramX_Array  所有头像的x坐标
 *  @param FramY_Array  所有头像的y坐标
 *  @param offsetX      x轴的偏移量+右，-左
 *  @param offsetY      y轴的偏移量
 *  @param imgNameArray 图片数组
 */
-(void)makeCircleAddScrollView:(UIScrollView *)scrollView WithFramXArray:(NSArray *)FramX_Array andFramYArray:(NSArray*)FramY_Array  WithOffsetWithX:(CGFloat)offsetX andOffsetY:(CGFloat)offsetY WithImageNameArray:(NSArray *)imgNameArray
{


    for (NSInteger i = 0; i < self.familyNumber; i++) {
        //拿到x坐标
        CGFloat framX = [FramX_Array[i] floatValue];
        //拿到y坐标
        CGFloat framY = [FramY_Array[i] floatValue];
        //1.创建底层btn
        UIButton * bottomHeaderBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        bottomHeaderBtn.frame = CGRectMake(framX+offsetX, framY+offsetY, HeaderPIC_Weight, headerPIC_Height);
        bottomHeaderBtn.backgroundColor = [UIColor clearColor];
        bottomHeaderBtn.tag = 250+i;
        [scrollView addSubview:bottomHeaderBtn];
        UIImageView * imgView = [[UIImageView alloc] initWithFrame:bottomHeaderBtn.bounds];
        //把有颜色的图片贴在btn上
        [imgView setImage:[UIImage imageNamed:imgNameArray[i]]];
        [bottomHeaderBtn addSubview:imgView];
        //button的触发事件
        [bottomHeaderBtn addTarget:self action:@selector(familyHeaderButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
        //给这个btn添加一个长安手势
        UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
        [bottomHeaderBtn addGestureRecognizer:longPress];
        
                
        //取出家人的数据--姓名 -- 头像
        AllFamilyModal * modal = self.AllMyFamilyArray[i];
        
        //2. 家人真实姓名
        UILabel * namelable = [[UILabel alloc] initWithFrame:CGRectMake((HeaderPIC_Weight-70)/2.0,53, 70, 16) ];
        namelable.text = modal.realname;
        namelable.font = [UIFont systemFontOfSize:11];
        namelable.textColor = [UIColor whiteColor];
        namelable.userInteractionEnabled = NO;
        namelable.textAlignment = NSTextAlignmentCenter;
        [bottomHeaderBtn addSubview:namelable];
        
        //3.创建img贴家人真实头像
        UIImageView * headerImg = [[UIImageView alloc] initWithFrame:bottomHeaderBtn.bounds];
        headerImg.backgroundColor = [UIColor clearColor];
        //切掉头像的边角
        headerImg.clipsToBounds = YES;
        headerImg.layer.cornerRadius = 34;
        headerImg.userInteractionEnabled = NO;
        [bottomHeaderBtn addSubview:headerImg];
        //把家人的头像贴在这个imgView上
        
        if ([modal.photo intValue] == 1) {
            NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
            NSFileManager *fileManager = [NSFileManager defaultManager];
            //
            NSString * headerImgFloder = [docPath stringByAppendingString:[NSString stringWithFormat:@"/FamilyHeaderFloder/"]];
            if (![fileManager fileExistsAtPath:headerImgFloder]) {
                [[NSFileManager defaultManager] createDirectoryAtPath:headerImgFloder withIntermediateDirectories:YES attributes:nil error:nil];
                //在创建文件夹的时候就把此时的时间保存下来
                NSUserDefaults * timeUser = [NSUserDefaults standardUserDefaults];
                //先判断是否已经有了保存文件的时间
                NSDate * oldTime = [timeUser objectForKey:@"FamilyHeaderFloderDate"];
                NSDate * nowDate = [NSDate date];
                //如果没有那么就把此时的时间保存下来
                if (oldTime == nil) {
                    [timeUser setObject:nowDate forKey:@"FamilyHeaderFloderDate"];
                    [timeUser synchronize];
                }
            }
            NSString * newPicPath = [headerImgFloder stringByAppendingString:[NSString stringWithFormat:@"headerImg%@%@.jpg",modal.uid,modal.fid]];
            if (![fileManager fileExistsAtPath:newPicPath]) {
                [headerImg setImageWithURL:[NSURL URLWithString:modal.avatar] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
                    CGRect range = CGRectMake(0, 0,image.size.height, image.size.height);
                    UIImage * downImg = [self imageFromImage:image inRect:range];
                    //把这个图片按这个图片的名字保存下来
                    [UIImageJPEGRepresentation(downImg, 0.5) writeToFile:newPicPath atomically:YES];
                }];
                
            }else{
                UIImage * localImg = [UIImage imageWithContentsOfFile:newPicPath];
                [headerImg setImage:localImg];
            }
        }
        
        //4.创建顶层的imgView贴镂空的图
        UIImageView * topCoverImgView = [[UIImageView alloc] initWithFrame:headerImg.bounds];
        [topCoverImgView setImage:[UIImage imageNamed:@"topCover.png"]];
        topCoverImgView.backgroundColor = [UIColor clearColor];
        [headerImg addSubview:topCoverImgView];
        
        
        //5创建抖动删除图片
        UIImageView *deleteImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"deleteFamily"]];
        deleteImageView.frame = CGRectMake(HeaderPIC_Weight-20,13, 20, 20);
        deleteImageView.hidden = YES;
        deleteImageView.tag = i+500;
        deleteImageView.userInteractionEnabled = YES;
        [bottomHeaderBtn addSubview:deleteImageView];
        [self.view bringSubviewToFront:deleteImageView];
        //删除手势
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(deleteFamily:)];
        tap.numberOfTapsRequired = 1;
        tap.view.tag = deleteImageView.tag;
        [deleteImageView addGestureRecognizer:tap];
    }
    
    
}


-(UIImage *)imageFromImage:(UIImage *)image inRect:(CGRect)rect {
    CGImageRef sourceImageRef = [image CGImage];
    CGImageRef newImageRef = CGImageCreateWithImageInRect(sourceImageRef, rect);
    UIImage *newImage = [UIImage imageWithCGImage:newImageRef];
    return newImage;
}


//点击家人之后的触发
-(void)familyHeaderButtonClicked:(UIButton*)button
{
    
    //判断是否在抖动，如果不是抖动状态就触发btn的触发方法，实现跳转
    if (self.isShake == NO)
    {

        TakeDrugReminderViewController * DrugAndMedicalRecord = [[TakeDrugReminderViewController alloc] init];
        DrugAndMedicalRecord.MedicalRecord = @"病历";
        DrugAndMedicalRecord.navBarTitle = @"我的家人";
        
        if (button.tag == 12345)//自己
        {
            UserModel * modal =[[UserManager shareInstance]getUserInfo];
            //如果是自己的话就不传了
            DrugAndMedicalRecord.peopleDic = @{@"realname":modal.realname,@"uid":modal.uid,@"fid":modal.uid,@"avatar":modal.avatar,@"username":modal.username,@"type":@"7",@"gender":modal.gender};
            
        }else
        {
            AllFamilyModal * modal = self.AllMyFamilyArray[button.tag - 250];
            DrugAndMedicalRecord.peopleDic = @{@"uid":modal.uid,@"fid":modal.fid,@"username":modal.username,@"realname":modal.realname,@"avatar":modal.avatar,@"type":modal.type,@"birthday":modal.birthday,@"gender":modal.gender,@"idcard":modal.idcard,@"mobile":modal.mobile};
        }
    
        [self.navigationController pushViewController:DrugAndMedicalRecord animated:YES];
    }

}

#pragma mark - 手势触发方法
//叉图手势--删除我的家人
-(void)deleteFamily:(UITapGestureRecognizer*)tap
{
    
    AllFamilyModal * modal = self.AllMyFamilyArray[tap.view.tag-500];
    MyNSLog("%@", modal.fid);
    //
    [self AFNetForDeleteFamily:modal.fid AndIndex:(tap.view.tag - 500)];
}
//button的长按手势
-(void)longPress:(UILongPressGestureRecognizer *)longPressGes
{
  
    if(longPressGes.state == UIGestureRecognizerStateBegan)
    {
        [self BeginWobble];
    }
}

#pragma mark -- 头像（抖动）效果
//触发-开始抖动
-(void)BeginWobble
{
  
    for (UIView *view in self.mainScrollView.subviews)
    {
        srand([[NSDate date] timeIntervalSince1970]);
        float rand=(float)random();
            CFTimeInterval t=rand*0.0000000001;
            [UIView animateWithDuration:0.1 delay:t options:0  animations:^
             {
                 view.transform=CGAffineTransformMakeRotation(-0.05);
             } completion:^(BOOL finished)
             {
                 [UIView animateWithDuration:0.1 delay:0 options:UIViewAnimationOptionRepeat|UIViewAnimationOptionAutoreverse|UIViewAnimationOptionAllowUserInteraction  animations:^
                  {
                      view.transform=CGAffineTransformMakeRotation(0.05);
                  } completion:^(BOOL finished) {}];
             }];
            self.isShake = YES;
            
            for (UIView *view2 in view.subviews) {
                if (view2.tag - 250 ==view.tag) {
                    view2.hidden = NO;
                };
            }
        }
}
//结束抖动

-(void)EndWobble
{
    for (UIView *view in self.mainScrollView.subviews)
    {
        [UIView animateWithDuration:0.1 delay:0 options:UIViewAnimationOptionAllowUserInteraction|UIViewAnimationOptionBeginFromCurrentState animations:^
         {
             view.transform=CGAffineTransformIdentity;
             for (UIView *view2 in view.subviews) {
                 if (view2.tag - 250 ==view.tag) {
                     view2.hidden = YES;
                 };
             }
             self.isShake = NO;
            
         } completion:^(BOOL finished) {}];
    }
}

#pragma mark - AFNet删除家人
-(void)AFNetForDeleteFamily:(NSString *)fid AndIndex:(NSInteger)index
{
    [self showActionView];
    NSString *postURL = [NSString stringWithFormat:@"%@user/deletehome/",kBaseURL];
    NSDictionary * deleteParameters = @{@"platform":kPlatform,@"version":kVersion,@"fid":fid};
    AFHTTPRequestOperationManager *manager =[AFHTTPRequestOperationManager manager];
    [manager POST:postURL parameters:deleteParameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [self hiddenActionView];
        MyNSLog("网络请求成功");
        NSNumber * statusNum = [responseObject objectForKey:@"status"];
        NSNumberFormatter * numberFormatter = [[NSNumberFormatter alloc] init];
        NSString * statusString = [numberFormatter stringFromNumber:statusNum];
        if ([statusString isEqualToString:@"0"]) {
            [self showCustomHudSingleLine:@"删除失败"];
            MyNSLog("删除失败");
            [self EndWobble];
            
            
        }else
        {
            //把家人的头像图片删除--先判断家人有无头像
            NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
            NSString *imgPath = [docPath stringByAppendingPathComponent:[NSString stringWithFormat:@"familyHeaderImg%@.png",fid]];
            NSFileManager *fileManager = [NSFileManager defaultManager];
            if ([fileManager fileExistsAtPath:imgPath]) {
                [[NSFileManager defaultManager] removeItemAtPath:imgPath error:nil];
            }
            [self EndWobble];
            //删除成功后重新舒新界面重新布局
//            [self AFNetRequestWithAllFamily];
            //先把scrollView上的所有view删除
            for (UIView * view in self.mainScrollView.subviews) {
                [view removeFromSuperview];
            }
            [self.AllMyFamilyArray removeObjectAtIndex:index];
            [self makeDataPrepareForMakeUI];
            
            
            
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        MyNSLog("删除家人网络--失败");
        [self showCustomHudSingleLine:@"删除失败"];
        [self EndWobble];
        [self hiddenActionView];
    }];
}


@end
